package org.example;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * 种植类型枚举类
 * 枚举项包含：枚举名（按规则生成）、编码（JSON中的key）、描述（JSON中的value）、颜色值（JSON中的bz）
 */
public enum PlantTypeEnum {

    // 1. 禾谷类作物相关（bz非null）
    WINTER_WHEAT("110102", "冬小麦", "#6DB36D"),
    SPRING_WHEAT("110103", "春小麦", "#6DB36D"),
    HIGHLAND_BARLEY("110104", "青稞", "#FAFA49"),
    YUAN_WHEAT("110105", "元麦", "#FAFA49"),
    NAKED_OATS("110106", "莜麦", "#FAFA49"),
    RYE("110107", "黑麦", "#FAFA49"),
    CORN("110108", "玉米", "#64F9F9"),
    SORGHUM("110109", "高梁", "#B6B6F9"),
    FOXTAIL_MILLET("110110", "粟", "#FAB4B4"),
    BROOMCORN_MILLET("110111", "稷", "#FAB4B4"),
    BARLEY("110112", "大麦", "#6DB36D"),
    QUINOA("110113", "藜麦", "#6DB36D"),
    EARLY_RICE("110121", "早稻", "#B2D66A"),
    MID_SEASON_RICE("110131", "中稻", "#B2D66A"),

    // 2. 豆类作物相关（bz非null）
    SOYBEAN("110201", "大豆", "#F9F9BA"),
    BROAD_BEAN("110202", "蚕豆", "#6DF9B3"),
    PEA("110203", "豌豆", "#6DF9B3"),
    MUNG_BEAN("110204", "绿豆", "#6DF9B3"),
    ADZUKI_BEAN("110205", "小豆", "#6DF9B3"),
    COWPEA("110206", "豇豆", "#6DF9B3"),
    COMMON_BEAN_CROP("110207", "菜豆（豆类作物）", "#6DF9B3"),
    CHICKPEA("110208", "鹰嘴豆", "#6DF9B3"),

    // 3. 根茎类作物相关（bz非null）
    SWEET_POTATO("110301", "甘薯", "#FAA249"),
    POTATO("110302", "马铃薯", "#FAA249"),
    YAM_BEAN("110303", "豆薯", "#FAA249"),
    CASSAVA("110304", "木薯", "#FAA249"),
    CHINESE_YAM("110305", "山药（薯蓣）", "#FAA249"),
    CANNA_EDULIS("110306", "蕉藕", "#FAA249"),
    TARO("110307", "芋", "#FAA249"),
    JERUSALEM_ARTICHOKE("110308", "菊芋", "#FAA249"),

    // 4. 纤维作物相关（bz非null）
    COTTON("120101", "棉花", "#B672F9"),
    HEMP("120102", "大麻", "#F9A0A0"),
    FLAX("120103", "亚麻", "#F9A0A0"),
    JUTE("120104", "黄麻", "#F9A0A0"),
    KENAF("120105", "红麻", "#F9A0A0"),
    SISAL("120106", "剑麻", "#F9A0A0"),
    ABACA("120107", "蕉麻", "#F9A0A0"),
    PINEAPPLE_FIBER("120108", "菠萝麻", "#F9A0A0"),
    APOCYNUM("120109", "罗布麻", "#F9A0A0"),

    // 5. 油料作物相关（bz非null）
    COLESEED("120201", "油菜", "#FA49FA"),
    PEANUT("120202", "花生", "#F9CF8F"),
    SESAME("120203", "芝麻", "#F5E6BF"),
    CASTOR_BEAN("120204", "蓖麻", "#80B4F9"),
    SUNFLOWER("120205", "向日葵", "#80B4F9"),
    YELLOW_MUSTARD("120206", "黄芥", "#80B4F9"),
    PERILLA_SEED("120207", "苏子（荏子）", "#80B4F9"),
    OLIVE("120208", "油橄榄", "#80B4F9"),
    OIL_PEONY("120209", "油用牡丹", "#80B4F9"),
    FLAXSEED("120210", "胡麻", "#80B4F9");


    /**
     * 枚举编码（对应JSON中的key）
     */
    private final String code;

    /**
     * 种植类型描述（对应JSON中的value）
     */
    private final String desc;

    /**
     * 颜色值（对应JSON中的bz，格式如#6DB36D）
     */
    private final String color;


    /**
     * 构造方法
     * @param code 编码（JSON的key）
     * @param desc 描述（JSON的value）
     * @param color 颜色值（JSON的bz）
     */
    PlantTypeEnum(String code, String desc, String color) {
        this.code = code;
        this.desc = desc;
        this.color = color;
    }


    // -------------------  getter方法  -------------------
    public String getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }

    public String getColor() {
        return color;
    }


    // -------------------  工具方法  -------------------

    /**
     * 根据编码（code）获取枚举项
     * @param code 枚举编码（JSON中的key）
     * @return 匹配的枚举项（无匹配时返回空Optional）
     */
    public static Optional<PlantTypeEnum> getByCode(String code) {
        if (code == null || code.trim().isEmpty()) {
            return Optional.empty();
        }
        return Arrays.stream(PlantTypeEnum.values())
                .filter(enumItem -> enumItem.getCode().equals(code.trim()))
                .findFirst();
    }


}